package saas.adapter.sdk.config;

import com.baozun.tenant.perception.condition.ConditionOnMvcClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import saas.adapter.sdk.core.interceptor.InboundHttpConversionTenantInterceptor;

/**
 * @title: OmsTenantAdapterInterceptorConfig
 * @description: 拦截器配置
 * @author: guan.xin
 * @create: 2021-11-12 14:46
 * @since: JDK8
 */

@Conditional(ConditionOnMvcClass.class)
@Configuration
public class OmsTenantAdapterInterceptorConfig implements WebMvcConfigurer {

    /**
     * mvc拦截器
     *
     * @return
     */
    @Bean
    public InboundHttpConversionTenantInterceptor inboundHttpConversionTenantInterceptor() {
        return new InboundHttpConversionTenantInterceptor();
    }

    /**
     * Feign拦截器
     *
     * @return
     */
//    @Bean
//    public OutboundFeignConversionTenantInterceptor outboundFeignConversionTenantInterceptor() {
//        return new OutboundFeignConversionTenantInterceptor();
//    }

    /**
     * 添加拦截器配置(执行顺序设为基础架构saas中间件拦截器之前生效)
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(inboundHttpConversionTenantInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/error")
                .order(Ordered.LOWEST_PRECEDENCE - 1000);
    }
}
